import { api } from './api'

export interface Feedback {
  id: number
  user_id: number
  feed_back_type: string
  content: string
  status: 'pending' | 'resolved'
  created_at: number
  updated_at: number
  deleted_at: number
}

export interface FeedbackListResponse {
  code: number
  data: {
    total: number
    data: Feedback[]
  }
  msg: string
  tracers: string
}

export interface FeedbackResponse {
  code: number
  data: Feedback
  msg: string
  tracers: string
}

export interface FeedbackSubmitData {
  feed_back_type: string
  content: string
  contact?: string
  images?: string[]
}

export const feedbackApi = {
  // 获取所有反馈列表
  async getAllFeedbacks(pageSize: number = 10, pageNumber: number = 1): Promise<FeedbackListResponse> {
    try {
      return await api.get<FeedbackListResponse>('/api/v1/feedbacks', {
        params: {
          page_size: pageSize,
          page_number: pageNumber
        }
      })
    } catch (error) {
      console.error('获取反馈列表失败:', error)
      throw error
    }
  },

  // 获取我的反馈列表
  async getFeedbacks(pageSize: number = 10, pageNumber: number = 1): Promise<FeedbackListResponse> {
    try {
      return await api.get<FeedbackResponse>('/api/v1/my-feedbacks', {
        params: {
          page_size: pageSize,
          page_number: pageNumber,
          sort_by: 'created_at',
          sort_order: 'desc'
        }
      })
    } catch (error) {
      console.error('获取反馈列表失败:', error)
      throw error
    }
  },

  // 提交反馈
  async submitFeedback(data: FeedbackSubmitData): Promise<any> {
    try {
      return await api.post<any>('/api/v1/feedbacks', data)
    } catch (error) {
      console.error('提交反馈失败:', error)
      throw error
    }
  }
}